package von.seiji.cn.structure;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.junit.Test;
import von.seiji.cn.LayerUtil;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: SeiJi
 * @DateTime: 2022/2/21 17:29
 * @Description: 此人很懒，未写描述！
 **/
public class Data {

    @Test
    public void aflkdjflj() {
        InputStream stream = ClassLoader.getSystemResourceAsStream("data.json");
        String read = IoUtil.read(stream, CharsetUtil.UTF_8);
        List<Map<String, Object>> maps = JSON.parseObject(read, new TypeReference<List<Map<String, Object>>>() {
        });
        maps.forEach(System.out::println);
        System.out.println("-----------------------------------");
        List<Map<String, Object>> toTree = toTree(maps, 0);
        toTree.forEach(System.out::println);
        System.out.println("+++++++++++++++++++++++++++++++++++");
        List<Map<String, Object>> mapList = toFlat(toTree);
        mapList.forEach(System.out::println);

    }

    private List<Map<String, Object>> toTree(List<Map<String, Object>> maps, Integer topId) {

        List<Map<String, Object>> parents = maps.stream().filter(item -> topId.equals(MapUtil.getInt(item, "parentId")))
                .map(item -> {
                    List<Map<String, Object>> collect = maps.stream().filter(item1 -> MapUtil.getInt(item, "deptId")
                            .equals(MapUtil.getInt(item1, "parentId"))).collect(Collectors.toList());
                    collect.forEach(item1 -> {
                        item1.put("children", toTree(maps, MapUtil.getInt(item1, "deptId")));
                    });
                    item.put("children", collect);
                    return item;
                }).collect(Collectors.toList());
        return parents;
    }

    private List<Map<String, Object>> toFlat(List<Map<String, Object>> maps) {
        List<Map<String, Object>> children = maps.stream().flatMap(item -> {
            List<Map<String, Object>> mapList = JSON.parseObject(MapUtil.getStr(item, "children"), new TypeReference<List<Map<String, Object>>>() {
            });
            item.put("children", Lists.<Map<String, Object>>newArrayList());
            return mapList.stream();
        }).collect(Collectors.toList());
        if (!children.isEmpty()) {
            maps.addAll(toFlat(children));
        }
        return maps;
    }

    @Test
    public void dlak() {
        InputStream stream = ClassLoader.getSystemResourceAsStream("data.json");
        String read = IoUtil.read(stream, CharsetUtil.UTF_8);
        List<JSONObject> maps = JSON.parseObject(read, new TypeReference<List<JSONObject>>() {
        });

        LayerUtil build = LayerUtil.builder().id("deptId").pid("parentId").children("children").build();
        List<JSONObject> tree = build.toTree(maps, "0");
        tree.forEach(System.out::println);

        List<JSONObject> flat = build.toFlat(tree);
        System.out.println("=====================================================");
        flat.forEach(System.out::println);


     /*   LayerUtil.Tree tree = LayerUtil.creator("deptId", "parentId", "children");
        List<Map<String, Object>> treeList = tree.build(maps, "0");
        treeList.forEach(System.out::println);
        System.out.println("=====================================================");
        LayerUtil.Flat flat = LayerUtil.creator("children");
        List<Map<String, Object>> flatList = flat.build(treeList);
        flatList.forEach(System.out::println);*/
    }


}
